using System.Windows;
using System.Windows.Browser;

namespace MoonRainbow.Input.Mouse
{
    internal class MouseLeftButtonUpBehavior : MouseBehavior
    {
        public MouseLeftButtonUpBehavior()
        {
            HtmlPage.Document.Body.AttachEvent("mouseup", MouseDown);
        }

        private void MouseDown(object sender, HtmlEventArgs e)
        {
            if (e.MouseButton != MouseButtons.Left) return;
            Application.Current.RootVisual.IsHitTestVisible = true;
            var point = GetPosition(HtmlPage.Plugin.Parent);
            if (point.X > e.ClientX ||
                point.X + Application.Current.Host.Content.ActualWidth < e.ClientX ||
                point.Y > e.ClientY ||
                point.Y + Application.Current.Host.Content.ActualHeight < e.ClientY)
                return;
            var args = new MouseEventDetectedEventArgs(new Point(e.ClientX - point.X, e.ClientY - point.Y));
            InvokeMouseEventDetected(args);
            Application.Current.RootVisual.IsHitTestVisible = !args.IsHandled;
        }
    }
}